package com.example.keli.ui.userinfo;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.example.keli.R;
import com.example.keli.api.RetrofitManager;
import com.example.keli.base.BaseActivity;
import com.example.keli.base.app.C;
import com.example.keli.base.utils.ToastUtil;
import com.flyco.roundview.RoundTextView;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import com.example.keli.api.http.api.BaseResponse;
import com.example.keli.api.http.api.subscriber.SubscriberListener;

/**
 * description: 修改昵称
 * author: Allen
 * date: 2019/3/25 15:13
 */
public class EditNameActivity extends BaseActivity {

    @BindView(R.id.nick_name)
    EditText nickName;
    @BindView(R.id.commit)
    RoundTextView commit;

    public static void start(Context context,String nickName) {
        Intent intent = new Intent(context, EditNameActivity.class);
        intent.putExtra("nick_name",nickName);
        context.startActivity(intent);
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_edit_nickname;
    }

    @Override
    protected void bindData() {
        abTitle.setText("修改昵称");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        nickName.setText(getIntent().getStringExtra("nick_name"));
    }

    @OnClick(R.id.commit)
    public void onViewClicked() {
        if(TextUtils.isEmpty(nickName.getText().toString())){
            ToastUtil.show("请输入昵称");
            return;
        }
        setNickName(nickName.getText().toString());
    }

    private void setNickName(final String nickname) {
        Map<String, String> map = new HashMap<>();
        map.put("nickname", nickname);
        RetrofitManager.ApiFunc(RetrofitManager.mApiFunc.setUser(map), new SubscriberListener<BaseResponse<Object>>() {
            @Override
            public void onSuccess(BaseResponse<Object> objectBaseResponse) {
                EventBus.getDefault().post(C.EVENT.UPDATE_USERINFO);
                finish();
            }

            @Override
            public void onFailer(String msg) {

            }

            @Override
            public void onTokenError() {

            }
        });
    }
}
